package com.see51.demo.see51demo;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.see51.demo.see51demo.R;
import com.see51.sdk.java.I51SeeListener;
import com.see51.sdk.java.Jni51SeeSdk;

import java.io.File;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;

public class DevStreamActivity extends AppCompatActivity implements  I51SeeListener {

    private String         m_strDevId;
    private String         m_strDevTurnIp ;
    int m_nObjId=0;
    FileOutputStream outputStream  = null; //new RandomAccessFile(file, "rwd");
    public void  On51SeeAv(int devHandel,
                           int  frame_type,
                           int	av_type,
                           int	size,
                           int	frameRate_samplingRate,
                           int	timestamp,
                           int	video_cap,
                           int	reserved, byte[] data)
    {

        Log.d("stream：","len:"+data.length);
        if(  outputStream!=null){

            try {
                outputStream .write(data);
            } catch (Exception e) {
                e.printStackTrace();
                Log.d("outputStream：","err");
            }
        }else{
            Log.d("outputStream：","null");
        }
    }
    //设备断开通知
    public void    OnClose(int devHandel )
    {


    }
    //文件下载完成通知
    public void    OnDownFileOver(int devHandel,String fileName)
    {


    }
    public void  SetObjectId(int Id)
    {
        m_nObjId=Id;
    }
    public int   GetObjectId()
    {
        return m_nObjId;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dev_stream);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Bundle bundle = getIntent().getExtras();
        CharSequence DevId = bundle.getCharSequence("DevId");
        m_strDevId=DevId.toString();
        CharSequence DevTurnIp = bundle.getCharSequence("DevTurnIp");
        m_strDevTurnIp  = DevTurnIp.toString();

        Jni51SeeSdk sdk = new Jni51SeeSdk();
        int devHandle=sdk.JniGetDevHandle(m_strDevId,m_strDevTurnIp);
        sdk.JniAdd51SeeListener(devHandle, ( I51SeeListener)this ,8);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
       int ll= sdk.JniReleaseDevHandle(devHandle);
        Log.d("xxx：",""+ll);
        sdk .JniSee51SDKRequestVideo(devHandle, 0);

        String path = "/storage/emulated/0/test.h264";
        try
        {
           outputStream = new FileOutputStream(path);
            // fos = this.openFileOutput( path , MODE_PRIVATE);//获得FileOutputStream
        } catch (Exception e)
        {
            e.printStackTrace();
        }
}
    @Override
    protected void onDestroy() {
        Jni51SeeSdk sdk = new Jni51SeeSdk();
        int devHandle=sdk.JniGetDevHandle(m_strDevId,m_strDevTurnIp);
        sdk.JniReleaseSeeListener( devHandle,( I51SeeListener)this);
        sdk.JniSee51SDKStopDevVideo( devHandle);
        if(  outputStream !=null) {
            try {
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        int ll= sdk.JniReleaseDevHandle(devHandle);
        Log.d("xxx：",""+ll);
        super.onDestroy();
    }
}
